package com.wangmin.intercept.manager;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wm on 2017/4/11.
 */
public class LocalActivityManager {
    private static LocalActivityManager activityManager;
    public String phoneNumber;
    private List<Activity> activities = null;
    private LocalActivityManager() {
        activities = new ArrayList<Activity>();
    }

    /**
     * 单例
     *
     * @return LocalActivityManager
     */
    public synchronized static LocalActivityManager getInstance() {
        if (activityManager == null) {
            activityManager = new LocalActivityManager();
        }
        return activityManager;
    }

    /**
     * 添加activity
     *
     * @param activity 要添加的activity
     */
    public void addActivity(Activity activity) {
        activities.add(activity);
        showCurrentActivitys();
    }

    public void removeActivity(Activity activity){
        if(activity == null){
            return;
        }
        activities.remove(activity);
    }
    /**
     * 删除最上面的activity
     */
    public void removeTop() {
        if (activities != null && activities.size() > 0) {
            activities.get(activities.size() - 1).finish();
            activities.remove(activities.size() - 1);
        }
        showCurrentActivitys();
    }

    /**
     * 删除所有activity
     */
    public void removeAll() {
        if (activities == null || activities.size() == 0) {
            return;
        }
        int i = 0;
        for (Activity activity : activities) {
            activity.finish();
            i++;
        }
        activities.clear();
        showCurrentActivitys();
    }

    /**
     * 删除指定名称的activity
     *
     * @param name activity 名称
     * @return 是否删除成功
     */
    public boolean RemoveActivityByName(String name) {
        boolean flag = false;
        Activity activity1 = null;
        int index = -1;
        for (int i = activities.size() - 1; i >= 0; i--) {
            Activity activity2 = activities.get(i);
            if (activity2.getClass().getName().equals(name)) {
                activity1 = activity2;
                index = i;
                break;
            }
        }
        if (index != -1) {
            activities.get(index).finish();
            activities.remove(index);
            flag = true;
        }
        showCurrentActivitys();
        return flag;
    }

    /**
     * 显示当前任务栈中的activity
     */
    public void showCurrentActivitys() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = activities.size() - 1; i >= 0; i--) {
            Activity activity2 = activities.get(i);
            stringBuilder.append(activity2.getClass().getName());
        }
    }

    /**
     * 跳转到登陆activity
     */
    public void enterLoginActivity() {
        if (activities == null || activities.size() == 0) {
            return;
        }
        int i = 0;
        for (i = activities.size() - 1; i >= 1; i--) {
            activities.get(i).finish();
        }
        Activity activity = activities.get(0);
        activities.clear();
        activities.add(activity);
        showCurrentActivitys();
    }

    public Activity getCurrentActivity(){
        if(activities == null||activities.size() == 0){
            return null;
        }
        return activities.get(activities.size()-1);
    }
}
